package com.xiaotu.mvc.controller;

import com.xiaotu.entity.Admin;
import com.xiaotu.entity.Student;
import com.xiaotu.service.AdminService;
import com.xiaotu.util.CrowdUtil;
import com.xiaotu.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class TestController {
    @Autowired
     private AdminService adminService;
    @RequestMapping("/test/ssm.html")
    public String testSSM(Model model, HttpServletRequest httpServletRequest){
       List<Admin> adminList = adminService.getAll();
       model.addAttribute("adminList", adminList);
        boolean b = CrowdUtil.judgeRequestType(httpServletRequest);
        if(b){
            System.out.println("是Ajax请求");
        }else{
            System.out.println("不是Ajax请求");
        }
       return "target";
    }
    @RequestMapping("/send/One.html")
    @ResponseBody
    public String testReceiveArrayOne(@RequestParam("array[]") List<Integer> array){
        for (Integer num: array) {
            System.out.println("num="+num);
        }
        return "success";
    }
    @RequestMapping("/send/Two.html")
    @ResponseBody
    public String testReceiveArrayTwo(@RequestBody List<Integer> array){
        for (Integer num: array) {
            System.out.println("num="+num);
        }
        return "success";
    }
    @RequestMapping("/send/Three.json")
    @ResponseBody
    public ResultEntity<Student> testReceiveObjectThree(@RequestBody Student requestBody,HttpServletRequest httpServletRequest){
        System.out.println(requestBody);
        boolean b = CrowdUtil.judgeRequestType(httpServletRequest);
        if(b){
            System.out.println("是Ajax请求");
        }else{
            System.out.println("不是Ajax请求");
        }
        return ResultEntity.success(requestBody);
    }
}
